package com.hy.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.usercenter.common.BaseResponse;
import com.hy.usercenter.common.ResultEnum;
import com.hy.usercenter.common.ResultUtils;
import com.hy.usercenter.exception.CommonException;
import com.hy.usercenter.mapper.UserTeamRelationMapper;
import com.hy.usercenter.model.domain.SysUser;
import com.hy.usercenter.model.domain.Team;
import com.hy.usercenter.model.domain.UserTeamRelation;
import com.hy.usercenter.model.dto.JoinTeamParam;
import com.hy.usercenter.model.dto.TeamQuery;
import com.hy.usercenter.model.dto.TeamUpdateRequest;
import com.hy.usercenter.model.vo.TeamUserVo;
import com.hy.usercenter.service.SysUserService;
import com.hy.usercenter.service.TeamService;
import com.hy.usercenter.service.UserTeamRelationService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Description
 * @Author minsf
 * @Date 2023/2/26
 */
@RestController
@RequestMapping("/team")
@CrossOrigin(origins = {"http://localhost:5173/","http://localhost:8080", "http://127.0.0.1:5173/"},
        allowCredentials = "true",
        methods = {RequestMethod.OPTIONS,RequestMethod.GET,RequestMethod.POST}
)
public class TeamController {

    @Autowired
    private TeamService teamService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private UserTeamRelationService userTeamRelationService;

    /**
     * 加入队伍
     * @return 是否成功加入
     */
    @PostMapping("joinToTeam")
    public BaseResponse<Boolean> joinToTeam(@RequestBody JoinTeamParam joinTeamParam) {
        if (ObjectUtils.anyNull(joinTeamParam, joinTeamParam.getTeamId(), joinTeamParam.getUserId())) {
            throw new CommonException(ResultEnum.PARAMS_ERROR, "无效参数");
        }
        Boolean result = userTeamRelationService.joinToTeam(joinTeamParam);
        return ResultUtils.success(result);
    }

    /**
     * 修改队伍信息
     *
     * @return
     */
    @PostMapping("editTeam")
    public BaseResponse<Boolean> editTeamInfo(@RequestBody TeamUpdateRequest teamUpdateRequest) {

        SysUser currentUserInfo = userService.getCurrentUserInfo();
        Boolean result = teamService.editTeamInfo(teamUpdateRequest, currentUserInfo);

        return ResultUtils.success(Boolean.TRUE);
    }

    // 查询队伍列表
    @GetMapping("/list")
    public BaseResponse<List<TeamUserVo>> list(TeamQuery teamQuery) {

        // 不让其查全部数据
        if (Objects.isNull(teamQuery)) {
            throw new CommonException(ResultEnum.PARAMS_ERROR);
        }

        Boolean isAdmin = userService.isAdmin();

        List<TeamUserVo> teamUserVos = teamService.listTeamQuery(teamQuery, isAdmin);

        return ResultUtils.success(teamUserVos);
    }

    // 查询队伍列表
    @GetMapping("/listPage")
    public BaseResponse<Page<Team>> listByPage(TeamQuery teamQuery) {

        // 不让其查全部数据
        if (Objects.isNull(teamQuery)) {
            throw new CommonException(ResultEnum.PARAMS_ERROR);
        }

        Team team = new Team();
        BeanUtils.copyProperties(teamQuery, team);
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>(team);
        return ResultUtils.success(teamService.page(new Page<Team>(teamQuery.getPageNum(), teamQuery.getPageSize()),
                queryWrapper));
    }

    // 根据ID查询队伍信息
    @GetMapping("/{id}")
    public BaseResponse<Team> getById(@PathVariable Long id) {

        if (Objects.isNull(id)) {
            throw new CommonException(ResultEnum.PARAMS_ERROR);
        }

        return ResultUtils.success(teamService.getById(id));
    }

    // 新增队伍
    @PostMapping("add")
    public BaseResponse<Long> add(@RequestBody Team team) {
        if (Objects.isNull(team)) {
            throw new CommonException(ResultEnum.PARAMS_ERROR);
        }
        SysUser currentUserInfo = userService.getCurrentUserInfo();
        Long aLong = teamService.addTeam(team, currentUserInfo);

        return ResultUtils.success(aLong);
    }

    // 修改队伍信息
    @PostMapping("/update")
    public BaseResponse<Boolean> update( @RequestBody TeamQuery teamQuery) {

        if (Objects.isNull(teamQuery) || Objects.isNull(teamQuery.getId())) {
            throw new CommonException(ResultEnum.PARAMS_ERROR);
        }

        boolean result  = teamService.teamUpdate(teamQuery);
        return ResultUtils.success(result);
    }

    // 删除队伍
    @PostMapping("/delete/{id}")
    public BaseResponse<Boolean> delete(@PathVariable Long id) {
        if (Objects.isNull(id)) {
            throw new CommonException(ResultEnum.PARAMS_ERROR);
        }
        boolean result = teamService.removeById(id);
        if (!result) {
            throw new CommonException(ResultEnum.DATABASE_ERROR);
        }
        return ResultUtils.success(true);
    }
}
